<template>
	<view class="mainBg h100">
		<navbar title="确认订单" />
		<view class="container">
			<view class="info">
				<image :src="info.imageUrl" mode=""></image>
				<view class="title">
					{{info.name}}
				</view>
				<view class="bottom">
					<view class="price">
						￥{{info.salePrice}} <text>￥{{info.listPrice}}</text>
					</view>
					<view class="right">
						{{info.learnTotal}}人正在学
					</view>
				</view>
			</view>
<!-- 			<view class="list">
				<view class="title">
					您所购买的为系列课程，本系列包含以下课程：
				</view>
				<view class="col">
					<item></item>
				</view>
				<view class="col">
					<item></item>
				</view>
				<view class="col">
					<item></item>
				</view>
			</view> -->
		</view>
		<view class="container statistics">
			<view class="col">
				总计
				<text>￥{{info.salePrice}}</text>
			</view>
			<!-- 		<view class="col">
				优惠券
				<view class="coupon">
					-￥58.00
					<image src="@/static/arrow-right.png" mode=""></image>
				</view>
			</view> -->
			<view class="col">
				应付
				<text style="color: red;">￥{{info.salePrice}}</text>
			</view>
		</view>
		<view class="container payment ">
			<view class="title">
				选择支付方式
			</view>
			<view class="col" @click="payType=`wxpay`">
				<view class="left">
					<image src="@/static/index/pay-icon3.png" mode=""></image> 微信支付
				</view>
				<image class="selected" src="@/static/index/pay-icon9.png" mode="" v-if="payType===`wxpay`"></image>
				<image class="selected" src="@/static/index/pay-icon10.png" mode="" v-else></image>
			</view>
			<view class="col" @click="payType=`alipay`">
				<view class="left">
					<image src="@/static/index/pay-icon2.png" mode=""></image> 支付宝支付
				</view>
				<image class="selected" src="@/static/index/pay-icon9.png" mode="" v-if="payType===`alipay`"></image>
				<image class="selected" src="@/static/index/pay-icon10.png" mode="" v-else></image>
			</view>
			<view class="col" @click="payType=`walletpay`">
				<view class="left">
					<image src="@/static/index/pay-icon6.png" mode=""></image> 钱包支付
				</view>
				<image class="selected" src="@/static/index/pay-icon9.png" mode="" v-if="payType===`walletpay`"></image>
				<image class="selected" src="@/static/index/pay-icon10.png" mode="" v-else></image>
			</view>
		</view>
		<view class="payInfo">
			<view class="price">
				￥{{info.salePrice}}
			</view>
			<view class="btn" @click="buyNow">
				立即购买
			</view>
		</view>
		<view class="seat">

		</view>
	</view>
</template>

<script>
	import item from "./item.vue"
	export default {
		components: {
			item
		},
		data() {
			return {
				info: {},
				courseType:"quality",
				payType: "wxpay",
				orderId:'',
			}
		},
		onLoad(option) {
			this.getInfo(option.id)
		},
		methods: {
			getInfo(id) {
				this.$post("/course/course_detail", {
					id: id
				}).then(res => {
					this.info = res.data
				})
			},
			buyNow() {
				uni.showLoading()
					this.$post("/order/place_order", {
						courseId:this.info.id,
						courseType:this.courseType,
						payType:this.payType
					}).then(res => {
						uni.hideLoading()
						
						if(res.code == 200)
						{
							this.orderId = res.data.orderId
							uni.showLoading({
								title:"支付中",
								mask:true,
							})
							this.startOrderPolling(this.orderId)
						}else{
							this.$toast(res.msg)
						}
					
					})
			},
			// 开始订单轮询
			  startOrderPolling(orderId) {
			    // 记录定时器ID
			    this.pollingTimer = setInterval(() => {
			      this.checkOrderStatus(orderId);
			    }, 3000); // 每3秒查询一次
			  },
			  // 停止订单轮询
			    stopOrderPolling() {
			      if (this.pollingTimer) {
			        clearInterval(this.pollingTimer);
			        this.pollingTimer = null;
			      }
				  // 手动关闭加载提示
				    uni.hideLoading();
							
			    },
			checkOrderStatus(orderId){
				this.$post("/recharge/query_order",{orderId:orderId}).then(res => {
					// console.log(res)
					if(res.code == 200)
					{
						let orderStatus = res.data.orderStatus
						if(orderStatus == "1" || orderStatus == "2")
						{
							this.stopOrderPolling();
							setTimeout(()=>{
								this.gotoOrder()
							},1500)
						}
					
						if(orderStatus == "1")
						{
							this.$toast("支付成功")
			
						}else if(orderStatus == "2")
						{
							this.$toast("支付失败")
						}
					}else{
						console.log("查询订单失败",res.msg)
					}
					
				})
			},
			gotoOrder(){
				 uni.redirectTo({
				    url: '/pages/user/order/list'
				  });
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #35CBAD;
	}

	.container {
		background-color: #fff;
		padding: 0 40rpx;
		border-radius: 8rpx;
		margin-bottom: 20rpx;
	}

	.info {
		margin-bottom: 50rpx;

		image {
			height: 280rpx;
			border-radius: 8rpx;
		}

		.title {
			font-weight: bold;
			padding: 10rpx 0;
		}

		.bottom {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 24rpx;
			color: #999;

			.price {
				color: red;
				font-weight: bold;
				font-size: 32rpx;

				text {
					font-size: 24rpx;
					color: #999;
					font-weight: normal;
					text-decoration: line-through;
				}
			}

			.right text {
				font-size: 24rpx;
				padding: 6rpx 10rpx;
				border: 2rpx solid $assist-color2;
				color: $assist-color2;
				border-radius: 100px;
			}
		}
	}

	.list {
		.title {
			font-weight: bold;
			font-size: 28rpx;
		}

		.col {
			padding: 20rpx 0;
		}
	}

	.statistics {
		padding-top: 10rpx;
		padding-bottom: 10rpx;

		.col {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;
			padding: 20rpx 0;

			.coupon {
				display: flex;
				align-items: center;

				image {
					width: 32rpx;
					height: 32rpx;
					margin-left: 6rpx;
				}
			}

		}
	}

	.payment {
		padding-top: 30rpx;
		padding-bottom: 10rpx;

		.title {
			font-weight: bold;
		}

		.col {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx 0;

			.left {
				display: flex;
				align-items: center;

				image {
					width: 64rpx;
					height: 64rpx;
				}
			}


			.selected {
				width: 54rpx;
				height: 54rpx;
			}
		}
	}

	.payInfo {
		width: 100%;
		height: 110rpx;
		position: fixed;
		left: 0%;
		bottom: 0;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 40rpx;
		box-shadow: 0 0 20rpx rgba($color: $main-color, $alpha: 0.2);

		.price {
			font-weight: bold;
			color: red;
			font-size: 40rpx;
		}

		.btn {
			background-color: $assist-color2;
			color: #fff;
			font-size: 24rpx;
			text-align: center;
			width: 160rpx;
			line-height: 60rpx;
			border-radius: 8rpx;
		}
	}

	.seat {
		height: 110rpx;
	}
</style>